package com.cci.kangdao.controller;

import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.service.DictionariesTService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 字典类controller
 *
 */
@Controller
public class DictionariesTController {
	
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private DictionariesTService dictionariesTService;

	//获取外出类型字典项
	@RequestMapping("/getGoOutTypeList.aspx")
	public ModelAndView getGoOutTypeList(HttpServletRequest request,
														   HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return dictionariesTService.getGoOutTypeList(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	//区域工程师远程解决时获取字典项
	@RequestMapping("/getDictionariesTDataByAreaEngineer.aspx")
	public ModelAndView getDictionariesTDataByAreaEngineer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return dictionariesTService.getDictionariesTDataByAreaEngineer(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}

	@RequestMapping("/getDictionaryList.aspx")
	public void getDictionaries(HttpServletRequest request, HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			dictionariesTService.getDictionaryList(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}

	}

	@RequestMapping("/getDictionaryListByDTableName.aspx")
	public void getDictionaryListByDTableName(HttpServletRequest request, HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			dictionariesTService.getDictionaryListByDTableName(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}

	}

	@RequestMapping("/getDictionaryTree.aspx")
	@ResponseBody
	public BaseResponse getDictionaryTree(String dTableName) {
		if (StringUtils.isBlank(dTableName)) {
			return BaseResponse.error(-99, "请输入dTableName");
		}
		return BaseResponse.success(dictionariesTService.getDictionaryTree(dTableName));
	}
}
